package com.tool.cn.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 根据网络地址和子网掩码计算下行IP地址范围
 * 网页：https://blog.csdn.net/whatday/article/details/79168699
 * @author wangzh
 * @date 2018年10月18日 下午3:59:54
 */
public class IpUtil {
	
	 /** 
     * 在main方法里面可以测试下 
     */  
    public static void main(String[] args) {  
        String ip="10.189.130.21";//ip  
        String mask="29";//位数，如果只知道子网掩码不知道位数的话在参考getMaskMap()方法  
          
        //获得起始IP和终止IP的方法（包含网络地址和广播地址）  
        String startIp=getBeginIpStr(ip, mask);  
        String endIp=getEndIpStr(ip, mask);  
        System.out.println("包含网络地址起始IP：" + startIp + "终止IP：" + endIp);  
          
        //获得起始IP和终止IP的方法（不包含网络地址和广播地址）  
        String subStart=startIp.split("\\.")[0]+"."+startIp.split("\\.")[1]+"."+startIp.split("\\.")[2]+".";  
        String subEnd=endIp.split("\\.")[0]+"."+endIp.split("\\.")[1]+"."+endIp.split("\\.")[2]+".";  
        startIp=subStart+(Integer.parseInt(startIp.split("\\.")[3])+1);  
        endIp=subEnd+(Integer.parseInt(endIp.split("\\.")[3])-1);  
        System.out.println("起始IP：" + startIp + "终止IP：" + endIp);  
          
        //判断一个IP是否属于某个网段  
        boolean flag = isInRange("10.2.0.0", "10.3.0.0/17");  
        System.out.println(flag);  
          
        //根据位数查询IP数量  
        int ipCount = getIpCount("8");  
        System.out.println(ipCount);  
          
        //判断是否是一个IP  
        System.out.println(isIP("192.168.1.0"));  
          
        //把ip转换为数字(mysql中inet_aton()的实现)  
        System.out.println(ipToDouble("192.168.1.1"));  
          

    }  

    /** 
     * 功能：判断一个IP是不是在一个网段下的 
     * 格式：isInRange("192.168.8.3", "192.168.9.10/22"); 
     */  
    public static boolean isInRange(String ip, String cidr) {    
        String[] ips = ip.split("\\.");    
        int ipAddr = (Integer.parseInt(ips[0]) << 24)    
                | (Integer.parseInt(ips[1]) << 16)    
                | (Integer.parseInt(ips[2]) << 8) | Integer.parseInt(ips[3]);    
        int type = Integer.parseInt(cidr.replaceAll(".*/", ""));    
        int mask = 0xFFFFFFFF << (32 - type);    
        String cidrIp = cidr.replaceAll("/.*", "");    
        String[] cidrIps = cidrIp.split("\\.");    
        int cidrIpAddr = (Integer.parseInt(cidrIps[0]) << 24)    
                | (Integer.parseInt(cidrIps[1]) << 16)    
                | (Integer.parseInt(cidrIps[2]) << 8)    
                | Integer.parseInt(cidrIps[3]);    
    
        return (ipAddr & mask) == (cidrIpAddr & mask);    
    }  
      
    /** 
     * 功能：根据IP和位数返回该IP网段的所有IP 
     * 格式：parseIpMaskRange("192.192.192.1.", "23") 
     */  
    public static List<String> parseIpMaskRange(String ip,String mask){  
        List<String> list=new ArrayList<>();  
        if ("32".equals(mask)) {  
            list.add(ip);  
        }else{  
            String startIp=getBeginIpStr(ip, mask);  
            String endIp=getEndIpStr(ip, mask);  
            if (!"31".equals(mask)) {  
                String subStart=startIp.split("\\.")[0]+"."+startIp.split("\\.")[1]+"."+startIp.split("\\.")[2]+".";  
                String subEnd=endIp.split("\\.")[0]+"."+endIp.split("\\.")[1]+"."+endIp.split("\\.")[2]+".";  
                startIp=subStart+(Integer.parseInt(startIp.split("\\.")[3])+1);  
                endIp=subEnd+(Integer.parseInt(endIp.split("\\.")[3])-1);  
            }  
            list=parseIpRange(startIp, endIp);  
        }  
        return list;  
    }  
      
    /** 
     * 功能：根据位数返回IP总数 
     * 格式：parseIpMaskRange("192.192.192.1", "23") 
     */  
    public static int getIpCount(String mask) {  
        return BigDecimal.valueOf(Math.pow(2, 32 - Integer.parseInt(mask))).setScale(0, BigDecimal.ROUND_DOWN).intValue();//IP总数，去小数点  
    }  
      
    /** 
     * 功能：根据位数返回IP总数 
     * 格式：isIP("192.192.192.1") 
     */  
    public static boolean isIP(String str) {    
        String regex = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";    
        Pattern pattern = Pattern.compile(regex);    
        return pattern.matcher(str).matches();    
    }    
      
    private static List<String> parseIpRange(String ipfrom, String ipto) {  
        List<String> ips = new ArrayList<String>();  
        String[] ipfromd = ipfrom.split("\\.");  
        String[] iptod = ipto.split("\\.");  
        int[] int_ipf = new int[4];  
        int[] int_ipt = new int[4];  
        for (int i = 0; i < 4; i++) {  
            int_ipf[i] = Integer.parseInt(ipfromd[i]);  
            int_ipt[i] = Integer.parseInt(iptod[i]);  
        }  
        for (int A = int_ipf[0]; A <= int_ipt[0]; A++) {  
            for (int B = (A == int_ipf[0] ? int_ipf[1] : 0); B <= (A == int_ipt[0] ? int_ipt[1]  
                    : 255); B++) {  
                for (int C = (B == int_ipf[1] ? int_ipf[2] : 0); C <= (B == int_ipt[1] ? int_ipt[2]  
                        : 255); C++) {  
                    for (int D = (C == int_ipf[2] ? int_ipf[3] : 0); D <= (C == int_ipt[2] ? int_ipt[3]  
                            : 255); D++) {  
                        ips.add(A + "." + B + "." + C + "." + D);  
                    }  
                }  
            }  
        }  
        return ips;  
    }  
      
	/**
	 * ipv4 点分转十进制
	 * @param ip
	 * @return
	 */
	public static long ipv4ToTen(String ip){
        String[] i=ip.split("\\.");
        long n=Long.parseLong(i[0]);
        for(int j=1;j<i.length;j++){
            n=n<<8;
            n+=Long.parseLong(i[j]);
        }
        return n;
    }
	
	/**
	 * ipv4 十进制转点分
	 * @param ip
	 * @return
	 */
	public static String tenToIpv4(long ten){
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<4;i++){
            sb.insert(0,Long.toString(ten&0xff));
            if(i<3){
                sb.insert(0,'.');
            }
            ten=ten>>8;
        }
        return sb.toString();
    }
    
    /** 
     * 根据掩码位获取掩码 
     * 
     * @param maskBit 
     *            掩码位数，如"28"、"30" 
     * @return 
     */  
    public static String getMaskByMaskBit(String maskBit)  
    {  
        return "".equals(maskBit) ? "error, maskBit is null !" : getMaskMap(maskBit);  
    }  
      
    /** 
     * 根据 ip/掩码位 计算IP段的起始IP 如 IP串 218.240.38.69/30 
     * 
     * @param ip 
     *            给定的IP，如218.240.38.69 
     * @param maskBit 
     *            给定的掩码位，如30 
     * @return 起始IP的字符串表示 
     */  
    public static String getBeginIpStr(String ip, String maskBit)  
    {  
        return tenToIpv4(getBeginIpLong(ip, maskBit));  
    }  
    /** 
     * 根据 ip/掩码位 计算IP段的起始IP 如 IP串 218.240.38.69/30 
     * 
     * @param ip 
     *            给定的IP，如218.240.38.69 
     * @param maskBit 
     *            给定的掩码位，如30 
     * @return 起始IP的长整型表示 
     */  
    public static Long getBeginIpLong(String ip, String maskBit)  
    {  
        return ipv4ToTen(ip) & ipv4ToTen(getMaskByMaskBit(maskBit));  
    }  
    /** 
     * 根据 ip/掩码位 计算IP段的终止IP 如 IP串 218.240.38.69/30 
     * 
     * @param ip 
     *            给定的IP，如218.240.38.69 
     * @param maskBit 
     *            给定的掩码位，如30 
     * @return 终止IP的字符串表示 
     */  
    public static String getEndIpStr(String ip, String maskBit)  
    {  
        return tenToIpv4(getEndIpLong(ip, maskBit));  
    }  
      
     /** 
     * 根据 ip/掩码位 计算IP段的终止IP 如 IP串 218.240.38.69/30 
     * 
     * @param ip 
     *            给定的IP，如218.240.38.69 
     * @param maskBit 
     *            给定的掩码位，如30 
     * @return 终止IP的长整型表示 
     */  
    public static Long getEndIpLong(String ip, String maskBit)  
    {  
        return getBeginIpLong(ip, maskBit)  
                + ~ipv4ToTen(getMaskByMaskBit(maskBit));  
    }  
      
      
      /** 
     * 根据子网掩码转换为掩码位 如 255.255.255.252转换为掩码位 为 30 
     * 
     * @param netmarks 
     * @return 
     */  
    public static int getNetMask(String netmarks)  
    {  
        StringBuilder sbf;  
        String str;  
        int inetmask = 0;  
        int count = 0;  
        String[] ipList = netmarks.split("\\.");  
        for (int n = 0; n < ipList.length; n++)  
        {  
            sbf = toBin(Integer.parseInt(ipList[n]));  
            str = sbf.reverse().toString();  
            count = 0;  
            for (int i = 0; i < str.length(); i++)  
            {  
                i = str.indexOf('1', i);  
                if (i == -1)  
                {  
                    break;  
                }  
                count++;  
            }  
            inetmask += count;  
        }  
        return inetmask;  
    }  
      
    /** 
     * 计算子网大小 
     * 
     * @param netmask 
     *            掩码位 
     * @return 
     */  
    public static int getPoolMax(int maskBit)  
    {  
        if (maskBit <= 0 || maskBit >= 32)  
        {  
            return 0;  
        }  
        return (int) Math.pow(2, 32 - maskBit) - 2;  
    }  
    private static StringBuilder toBin(int x)  
    {  
        StringBuilder result = new StringBuilder();  
        result.append(x % 2);  
        x /= 2;  
        while (x > 0)  
        {  
            result.append(x % 2);  
            x /= 2;  
        }  
        return result;  
    }  
      
    public static String getMaskMap(String maskBit) {  
        if ("1".equals(maskBit)) return "128.0.0.0";  
        if ("2".equals(maskBit)) return "192.0.0.0";  
        if ("3".equals(maskBit)) return "224.0.0.0";  
        if ("4".equals(maskBit)) return "240.0.0.0";  
        if ("5".equals(maskBit)) return "248.0.0.0";  
        if ("6".equals(maskBit)) return "252.0.0.0";  
        if ("7".equals(maskBit)) return "254.0.0.0";  
        if ("8".equals(maskBit)) return "255.0.0.0";  
        if ("9".equals(maskBit)) return "255.128.0.0";  
        if ("10".equals(maskBit)) return "255.192.0.0";  
        if ("11".equals(maskBit)) return "255.224.0.0";  
        if ("12".equals(maskBit)) return "255.240.0.0";  
        if ("13".equals(maskBit)) return "255.248.0.0";  
        if ("14".equals(maskBit)) return "255.252.0.0";  
        if ("15".equals(maskBit)) return "255.254.0.0";  
        if ("16".equals(maskBit)) return "255.255.0.0";  
        if ("17".equals(maskBit)) return "255.255.128.0";  
        if ("18".equals(maskBit)) return "255.255.192.0";  
        if ("19".equals(maskBit)) return "255.255.224.0";  
        if ("20".equals(maskBit)) return "255.255.240.0";  
        if ("21".equals(maskBit)) return "255.255.248.0";  
        if ("22".equals(maskBit)) return "255.255.252.0";  
        if ("23".equals(maskBit)) return "255.255.254.0";  
        if ("24".equals(maskBit)) return "255.255.255.0";  
        if ("25".equals(maskBit)) return "255.255.255.128";  
        if ("26".equals(maskBit)) return "255.255.255.192";  
        if ("27".equals(maskBit)) return "255.255.255.224";  
        if ("28".equals(maskBit)) return "255.255.255.240";  
        if ("29".equals(maskBit)) return "255.255.255.248";  
        if ("30".equals(maskBit)) return "255.255.255.252";  
        if ("31".equals(maskBit)) return "255.255.255.254";  
        if ("32".equals(maskBit)) return "255.255.255.255";  
        return "-1";  
    }  
    
    /**
     * ip转换数字
     */
    public static double ipToDouble(String ip) {  
        String[] arr = ip.split("\\.");  
        double d1 = Double.parseDouble(arr[0]);  
        double d2 = Double.parseDouble(arr[1]);  
        double d3 = Double.parseDouble(arr[2]);  
        double d4 = Double.parseDouble(arr[3]);  
        return d1 * Math.pow(256, 3) + d2 * Math.pow(256, 2) + d3 * 256 + d4;  
    }
    
    /**
     * 获取本机真实ip地址，获取失败则返回为空
     */
    public static String getLocalHostIp()  {
	    try {
	        InetAddress candidateAddress = null;
	        // 遍历所有的网络接口
	        for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
	            NetworkInterface iface =  ifaces.nextElement();
	            // 在所有的接口下再遍历IP
	            for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
	                InetAddress inetAddr =  inetAddrs.nextElement();
	                if (!inetAddr.isLoopbackAddress()) {// 排除loopback类型地址
	                    if (inetAddr.isSiteLocalAddress()) {
	                        // 如果是site-local地址，就是它了
	                        return inetAddr.getHostAddress();
	                    } else if (candidateAddress == null) {
	                        // site-local类型的地址未被发现，先记录候选地址
	                        candidateAddress = inetAddr;
	                    }
	                }
	            }
	        }
	        if (candidateAddress != null) {
	            return candidateAddress.getHostAddress();
	        }
	        // 如果没有发现 non-loopback地址.只能用最次选的方案
	        InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
	        if (jdkSuppliedAddress == null) {
	            throw new UnknownHostException("The JDK InetAddress.getLocalHost() method unexpectedly returned null.");
	        }
	        return jdkSuppliedAddress.getHostAddress();
	    } catch (Exception e) {
	        System.out.println("Failed to determine LAN address: " + e);
	        return "";
	    }
	}
    
    //--------------------------------实现ipv6和ipv4相关功能-------------------------------
    
	private static final Logger log = LoggerFactory.getLogger(IpUtil.class);
	
	private static String regexV4 = "(?:(?:1[0-9][0-9]\\.)|(?:2[0-4][0-9]\\.)|(?:25[0-5]\\.)|(?:[1-9][0-9]\\.)|(?:[0-9]\\.)){3}(?:(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5])|(?:[1-9][0-9])|(?:[0-9]))";
	private static String regexIpv6="^\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1"
			+ "\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:)"
			+ "{4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|"
			+ "((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:"
			+ "((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|"
			+ "1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|"
			+ "1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?\\s*$";

	/**
	 * 功能：字符串是否为ip 格式：isIP("192.192.192.1")
	 */
	private static boolean isIPv4(String str) {
		return Pattern.compile(regexV4).matcher(str).matches();
	}

	/**
	 * 验证ip掩码是否满足ip
	 * @param ipMask ip掩码
	 * @param ip ip地址
	 * @return
	 */
	public static boolean checkIp(String ipMask,String ip) {
		String tmpIp=ipMask.replaceAll("/.*", "");
		if(isIPv4(tmpIp)&&isIPv4(ip)) {
			return ipv4Contains(ip, ipMask);
		}else if(isIpv6(tmpIp)&&isIpv6(ip)){
			return ipv6Contains(tmpIp, ipMask);
		}
		
		return false;
	}
	
	/**
	 * ipv4 判断ip是否属于ip+掩码
	 * 
	 * @param ip     待判断ip 192.168.8.3
	 * @param ipMask ip加掩码，是个ip/掩码，192.168.9.10/22
	 * @return
	 */
	private static boolean ipv4Contains(String ip, String ipMask) {
		String[] ips = ip.split("\\.");
		int ipAddr = (Integer.parseInt(ips[0]) << 24) | (Integer.parseInt(ips[1]) << 16)
				| (Integer.parseInt(ips[2]) << 8) | Integer.parseInt(ips[3]);
		int type = Integer.parseInt(ipMask.replaceAll(".*/", ""));
		int mask = 0xFFFFFFFF << (32 - type);
		String cidrIp = ipMask.replaceAll("/.*", "");
		String[] cidrIps = cidrIp.split("\\.");
		int cidrIpAddr = (Integer.parseInt(cidrIps[0]) << 24) | (Integer.parseInt(cidrIps[1]) << 16)
				| (Integer.parseInt(cidrIps[2]) << 8) | Integer.parseInt(cidrIps[3]);

		return (ipAddr & mask) == (cidrIpAddr & mask);
	}

	/**
	 * 判断是否为ipv6
	 * 
	 * @param ipStr
	 * @return
	 */
	private static boolean isIpv6(String ipStr) {
		return Pattern.compile(regexIpv6).matcher(ipStr).matches();
	}

	/**
	 * ipv6 判断ip是否属于ip+掩码
	 * 
	 * @param ip     待判断 2001:0410:0000:0001:0000:0000:0000:45ff
	 * @param ipMask ip加掩码，是个ip/掩码，2001:0410:0000:0001:0000:0000:0000:45ff/64
	 * @return
	 */
	private static boolean ipv6Contains(String ip, String ipMask) {
		try {
			String resultIp = ipv6ToBinary(ip);
			int mask = Integer.parseInt(ipMask.replaceAll(".*/", ""));
			String realIp = ipv6ToBinary(ipMask.replaceAll("/.*", ""));

			return resultIp.subSequence(0, mask).equals(realIp.subSequence(0, mask));
		} catch (Exception e) {
			log.error(e.toString(),e);
			return false;
		}
	}

	/**
	 * ipv6 转 二进制字符串
	 * 
	 * @param ip
	 * @return
	 */
	private static String ipv6ToBinary(String ip) {
		String data = "", tmp;
		StringBuilder bud;
		String[] list = ip.split("\\:", -1);
		for (int i = 0; i < list.length; i++) {
			if (list[i]==null||list[i].isEmpty()) {
				list[i] = "0";
			}
			
			tmp = Integer.toBinaryString(Integer.valueOf(list[i], 16));
			if (tmp.length() < 16) {
				
				bud=new StringBuilder();
				int size = 16 - tmp.length();
				for (; size > 0; size--) {
					bud.append("0");
				}
				tmp=bud.toString()+tmp;
			}
			data += tmp;
		}
		return data;
	}

	/**
	 * ipv6 转 十进制
	 * 
	 * @param ip
	 * @return
	 */
	public static String ipv6ToDec(String ip) {

		String[] list = ip.split("\\:", -1);
		BigInteger big = BigInteger.ZERO;
		
		for (int i = 0; i < list.length; i++) {
			// ::1
			if (list[i].isEmpty()) {
				list[i] = "0";
			}
			big = big.add(BigInteger.valueOf(Integer.valueOf(list[i], 16)).shiftLeft(16 * (list.length - i - 1)));
		}

		return big.toString();
	}
    

}
